/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.mlu.jdbc.simple;

import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.Properties;
import java.util.ResourceBundle;
import javax.sql.DataSource;
import org.mlu.jdbc.simple.utils.PropertiesUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author Mlungisi Sincuba
 * @since 1.0
 */
public class DataSourceDetails {
    
    private static Logger logger = LoggerFactory.getLogger(DataSourceDetails.class);
    private static DataSource dataSource;
    private static final DataSourceDetails dataSourceDetails = new DataSourceDetails();

    private DataSourceDetails() {
    }
    
    public static DataSourceDetails getInstance() {
        return dataSourceDetails;
    }
    
    public static DataSource getDataSource() throws FileNotFoundException{
        if (dataSource == null) {
            logger.debug("Finding DataSource from project path on db.properties");
            Properties p = PropertiesUtils.getPropertiesFile("db.properties");
            String dataSourceName = "ThriftPool";
            String driver = p.getProperty("driver", "org.postgresql.Driver");
            String url = p.getProperty("url", "jdbc:postgresql://localhost:4444/gain");
            String user = p.getProperty("user", "gain");
            String pwd = p.getProperty("pwd", "gain");
            int minPool = Integer.parseInt(p.getProperty("minPool", "1"));
            int maxPool = Integer.parseInt(p.getProperty("maxPool", "15"));
            int maxSize = Integer.parseInt(p.getProperty("maxSize", "30"));
            ConnectionPool pool = new ConnectionPool(dataSourceName, driver, url, user, pwd, minPool, maxPool, maxSize);
            dataSource = pool.getDataSource();
        }
        return dataSource;
    }
    
    public static DataSource getDataSource(String fileName) throws FileNotFoundException{
        if (dataSource == null) {
            Properties p = PropertiesUtils.getPropertiesFile(fileName);
            String dataSourceName = "ThriftPool";
            String driver = p.getProperty("driver", "org.postgresql.Driver");
            String url = p.getProperty("url", "jdbc:postgresql://localhost:4444/gain");
            String user = p.getProperty("user", "gain");
            String pwd = p.getProperty("pwd", "gain");
            int minPool = Integer.parseInt(p.getProperty("minPool", "1"));
            int maxPool = Integer.parseInt(p.getProperty("maxPool", "15"));
            int maxSize = Integer.parseInt(p.getProperty("maxSize", "30"));
            ConnectionPool pool = new ConnectionPool(dataSourceName, driver, url, user, pwd, minPool, maxPool, maxSize);
            dataSource = pool.getDataSource();
        }
        return dataSource;
    }
    
    public static DataSource getDataSourceForWeb(InputStream is) {
        if (dataSource == null) {
            logger.debug("Getting DataSource from is: {}" + is);
            Properties p = PropertiesUtils.getPropertiesFile(is);
            String dataSourceName = "ThriftPool";
            String driver = p.getProperty("driver", "org.postgresql.Driver");
            String url = p.getProperty("url", "jdbc:postgresql://localhost:4444/gain");
            String user = p.getProperty("user", "gain");
            String pwd = p.getProperty("pwd", "gain");
            int minPool = Integer.parseInt(p.getProperty("minPool", "1"));
            int maxPool = Integer.parseInt(p.getProperty("maxPool", "15"));
            int maxSize = Integer.parseInt(p.getProperty("maxSize", "30"));
            ConnectionPool pool = new ConnectionPool(dataSourceName, driver, url, user, pwd, minPool, maxPool, maxSize);
            dataSource = pool.getDataSource();
        }
        return dataSource;
    }
    
    public static DataSource getDataSourceFromClassPath() {
        if (dataSource == null) {
            logger.debug("Getting DataSource from class path for filename db.properties");
            ResourceBundle rb = ResourceBundle.getBundle("db");
            String dataSourceName = "ThriftPool";
            String driver = rb.getString("driver") == null ? "org.postgresql.Driver" : rb.getString("driver");
            String url = rb.getString("url") == null ? "jdbc:postgresql://localhost:4444/gain" : rb.getString("url");
            String user = rb.getString("user") == null ? "gain" : rb.getString("user");
            String pwd = rb.getString("pwd") == null ? "gain" : rb.getString("pwd");
            int minPool = rb.getString("minPool") == null ? 1 : Integer.parseInt(rb.getString("minPool"));
            int maxPool = rb.getString("maxPool") == null ? 10 : Integer.parseInt(rb.getString("maxPool"));
            int maxSize = rb.getString("maxSize") == null ? 30 : Integer.parseInt(rb.getString("maxSize"));
            ConnectionPool pool = new ConnectionPool(dataSourceName, driver, url, user, pwd, minPool, maxPool, maxSize);
            dataSource = pool.getDataSource();
        }
        return dataSource;
    }
    
    public static DataSource getDataSourceFromClassPath(String fileNameWithoutExtention) {
        if (dataSource == null) {
            logger.debug("Getting DataSource from class path for filename db.properties");
            ResourceBundle rb = ResourceBundle.getBundle(fileNameWithoutExtention);
            String dataSourceName = "ThriftPool";
            String driver = rb.getString("driver") == null ? "org.postgresql.Driver" : rb.getString("driver");
            String url = rb.getString("url") == null ? "jdbc:postgresql://localhost:4444/gain" : rb.getString("url");
            String user = rb.getString("user") == null ? "gain" : rb.getString("user");
            String pwd = rb.getString("pwd") == null ? "gain" : rb.getString("pwd");
            int minPool = rb.getString("minPool") == null ? 1 : Integer.parseInt(rb.getString("minPool"));
            int maxPool = rb.getString("maxPool") == null ? 10 : Integer.parseInt(rb.getString("maxPool"));
            int maxSize = rb.getString("maxSize") == null ? 30 : Integer.parseInt(rb.getString("maxSize"));
            ConnectionPool pool = new ConnectionPool(dataSourceName, driver, url, user, pwd, minPool, maxPool, maxSize);
            dataSource = pool.getDataSource();
        }
        return dataSource;
    }
}
